package com.example.android.services

import android.util.Log
import com.example.android.extensions.StringBody
import com.example.android.models.Announce
import com.example.android.utils.Api
import com.example.android.utils.JsonUtil
import com.example.android.utils.models.Form
import com.example.android.utils.models.MyResult
import com.example.android.viewmodels.AnnounceViewModel

/**
 * @ClassName AnnounceService
 * @Author Ryan Kamanri
 * @Date 2022/6/1
 * @Description
 **/
class AnnounceService(private val _Api: Api, private val _JsonUtil: JsonUtil) {
    suspend fun AppendAnnounce(announceList: ArrayList<Announce>): MyResult<Unit> {
        val resJson = _Api.Get("/report/select").StringBody()
        Log.d(toString(), resJson)

        val result = _JsonUtil.FromJsonObject(resJson).AsMyResult<ArrayList<Form>>()

        result.Data?.forEach {
            announceList.add(Announce(it["content"] as String, it["id"] as Int, it["time"] as String, it["title"] as String))
        }

        return MyResult(MyResult.OK, result.Message)
    }

    suspend fun AppendNews(newsList: ArrayList<Announce>): MyResult<Unit> {
        val resJson = _Api.Get("/school-information/select").StringBody()

        val result = _JsonUtil.FromJsonObject(resJson).AsMyResult<ArrayList<Form>>()

        result.Data?.forEach {
            newsList.add(Announce(it["content"] as String, it["id"] as Int, it["time"] as String, it["title"] as String))
        }

        return MyResult(MyResult.OK, result.Message)
    }

    suspend fun AppendNews(model: AnnounceViewModel): MyResult<Unit> {
        val resJson = _Api.Get("/school-information/select").StringBody()

        val result = _JsonUtil.FromJsonObject(resJson).AsMyResult<ArrayList<Form>>()

        result.Data?.forEach {
            model.FocusItem.postValue(Announce(it["content"] as String, it["id"] as Int, it["time"] as String, it["title"] as String))
        }

        return MyResult(MyResult.OK, result.Message)
    }
}